home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / shadow-i.002 / shadow-i / shadow-ina-box-1.1 / adduser-shadow.pl next >
Perl Script  |  1996-06-13  |  4KB  |  147 lines

  1. #!/usr/bin/perl -w
  2. #
  3. # A perl5 script for interactive addition of user accounts
  4. # using the shadow password suite.
  5. #
  6. # Written by David Nugent <davidn@blaze.net.au>
  7. #
  8. # Prerequisites:
  9. #  Perl 5.001m or later
  10. #  Shadow password suite (specifically, adduser and
  11. #
  12. # 01 Apr 1996    Script written
  13. # 07 Apr 1996    Added logging feature
  14.  
  15. require 5.001;
  16.  
  17. my $version='1.01';
  18. my $program='Adduser';
  19. my $rcsdate='07-Apr-1996';
  20. my $log='/var/log/adduser';
  21.  
  22. print "\n$program: version $version\n";
  23.  
  24. die "You must be root to run this script." unless $< == 0;
  25.  
  26. my %add = {};
  27.  
  28. # Set some reasonable defaults
  29.  
  30. $add{'GROUP'}='users';
  31. $add{'HOME'}='/home';
  32. $add{'INACTIVE'}='0';
  33. $add{'EXPIRE'}='30';
  34. $add{'SHELL'}='/bin/sh';
  35. $add{'SKEL'}='/etc/skel';
  36.  
  37. umask(022);
  38.  
  39. # Then pick up the rest from useradd -D
  40.  
  41. if (open(CONFIG,'/etc/default/useradd')) {
  42.     while (<CONFIG>) {
  43.         chomp;
  44.         if (/([A-Z]+)=(.*)$/) {
  45.             $add{$1}=$2;
  46.         }
  47.     }
  48.     close CONFIG;
  49. }
  50.  
  51. while (1) {
  52.     my $switches = "-m -k $add{'SKEL'}";
  53.     my $logname = prompt("\nNew user login name");
  54.     last unless $logname;
  55.     if (getpwnam($logname)) {
  56.         print "That login name is already in use. Choose another.\n";
  57.         next;
  58.     }
  59.     my $uid = prompt("User id", nextuid());
  60.     if (getpwuid($uid)) {
  61.         my $res=substr(prompt("This userid is already in use - Ok", "n"),0,1);
  62.         next unless $res eq 'y' or $res eq 'Y';
  63.         $switches .= ' -o';
  64.     }
  65.     $switches .= " -u $uid";
  66.     my $comment = prompt("User's name");
  67.     next unless $comment;
  68.     $switches .= " -c \"$comment\"";
  69.   getgroup:
  70.     my $group = prompt("Group", $add{'GROUP'});
  71.     next unless $group;
  72.     if (!getgrnam($group) && !getgrgid($group)) {
  73.         print "That group does not exist\n";
  74.         goto getgroup;
  75.     }
  76.     $switches .= " -g $group";
  77.     my $home = prompt("Home directory", $add{'HOME'} . "/$logname");
  78.     $switches .= " -d $home";
  79.   getshell:
  80.     my $shell = prompt("Login shell", $add{'SHELL'});
  81.     my $shellok = 1;
  82.     if (open(SHELLS, '/etc/shells')) {
  83.         $shellok = 0;
  84.         while (<SHELLS>) {
  85.             chomp;
  86.             $shellok = 1 if $shell eq $_;
  87.         }
  88.         close SHELLS;
  89.         if (!$shellok) {
  90.             my $res = substr(prompt("'$shell' is not listed in /etc/shells. Ok",'n'), 0, 1);
  91.             goto getshell unless $res eq 'y' or $res eq 'Y';
  92.         }
  93.     }
  94.     $switches .= " -s $shell";
  95.     $switches .= " -e $add{'EXPIRE'}" if $add{'EXPIRE'};
  96.     print "\n----- NEW ACCOUNT DETAILS TO BE ADDED -----\n\n";
  97.     print "Account: $logname    [ $uid / $group ]\n";
  98.     print "   Name: $comment\n";
  99.     print "   Home: $home\n";
  100.     print "  Shell: $shell\n\n";
  101.     my $res = substr(prompt("Ok to proceed?", 'n'), 0, 1);
  102.     next unless $res eq 'y' or $res eq 'Y';
  103.     my $rc=system("useradd $switches $logname");
  104.     if ($rc == '0') {
  105.         if (open(ADDLOG, ">>$log")) {
  106.             my $tm = localtime time;
  107.             $tm .= sprintf(" Account %s id=%s gr=%s '%s' created by %s",
  108.                 $logname, $uid, $group, $comment, $ENV{'USER'});
  109.             print ADDLOG "$tm\n";
  110.             close ADDLOG
  111.             }
  112.         print "Account successfully added.\n";
  113.         print "You must now enter a password for this account.\n";
  114.         system("passwd $logname");
  115.     }
  116.     print "\nPress ENTER to quit adduser.\n";
  117. }
  118.  
  119. print "Adduser done.\n";
  120. exit 0;
  121.  
  122. sub prompt {
  123.     my ($quest,$def)=@_;
  124.     $quest .= ' [' . $def . ']' if $def;
  125.     print "$quest: ";
  126.     my $res = <STDIN>;
  127.     chomp $res;
  128.     $res = $def if $def && !$res;
  129.     return $res;
  130. }
  131.  
  132. sub nextuid {
  133.     my $nextid=0;
  134.     setpwent();
  135.     while (1) {
  136.         my ($name,$pwd,$uid,$gid,$gid,$quota,$comment,$gcos,$dir,$shell)
  137.             =getpwent;
  138.         last unless defined $name;
  139.         $nextid=int($uid + 1) unless ($nextid > $uid) or ($uid > 32767);
  140.     }
  141.     endpwent();
  142.     $nextid=100 unless $nextid;
  143.     return $nextid;
  144. }
  145.  
  146. __END__
  147.